/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RelatorioDept.java
 *
 * Created on 05/12/2011, 09:17:32
 */
package proj5labbd.graphics.relatorios;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import oracle.jdbc.OracleTypes;
import proj5labbd.DBInterface;
import proj5labbd.graphics.MainFrame;

/**
 *
 * @author yole
 */
public class RelatorioDept extends JFrame {

    private final int width = 700;
    private final int height = 700;
    
    /** Creates new form RelatorioDept */
    private DBInterface dbInterface;
    private String nomeDe;
    public RelatorioDept(DBInterface dbInterface, String nome) {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((dim.width-width)/2,(dim.height-height)/2,width,height);
        this.nomeDe = nome;
        this.dbInterface = dbInterface;
        try {
            // TODO add your handling code here:
            setupTable();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível gerar relatório");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbRelDept = new javax.swing.JTable();
        jlbExp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jtbRelDept.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbRelDept);

        jlbExp.setText("Relatório sobre os funcionários do departamento escolhido.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jlbExp, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                .addGap(12, 12, 12))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlbExp, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlbExp;
    private javax.swing.JTable jtbRelDept;
    // End of variables declaration//GEN-END:variables
    
    /*
     * Método para manipular a função relatorio_departamento e exibir seu retorno
     */
    private void setupTable() throws SQLException {
        CallableStatement stmt = dbInterface.getConn().prepareCall("{call ? := RELATORIO_DEPARTAMENTO(?)}");
        stmt.registerOutParameter(1, OracleTypes.CURSOR);
        stmt.setString(2, nomeDe);
        
        stmt.execute();
        ResultSet rs = (ResultSet)stmt.getObject(1);
        
        String [][] sArray = dbInterface.rsToStringArray(rs);
        
        DefaultTableModel model = new DefaultTableModel(sArray,
                new String[]{"Nome","CPF","Cargo","Salario","Nro. de dependentes"});
        
        jtbRelDept.setModel(model);
        
    }
}

